/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class ConvQueryResults
/*     */   extends ZimbraQueryResultsImpl
/*     */ {
/*     */   private final ZimbraQueryResults results;
/*     */   private ZimbraHit nextHit;
/*  39 */   private int nextHitNo = 0;
/*  40 */   private List<ZimbraHit> cachedResults = new ArrayList();
/*  41 */   private Set<Integer> seenConvIDs = new HashSet();
/*     */   
/*     */   ConvQueryResults(ZimbraQueryResults results, Set<MailItem.Type> types, SortBy sort, SearchParams.Fetch fetch) {
/*  44 */     super(types, sort, fetch);
/*  45 */     this.results = results;
/*     */   }
/*     */   
/*     */   public long getCursorOffset()
/*     */   {
/*  50 */     return this.results.getCursorOffset();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private ZimbraHit internalGetNextHit()
/*     */     throws ServiceException
/*     */   {
/*  59 */     while (this.results.hasNext()) {
/*  60 */       ZimbraHit opNext = this.results.getNext();
/*     */       
/*  62 */       if (!isConversation(opNext)) {
/*  63 */         return opNext;
/*     */       }
/*     */       
/*  66 */       int convId = opNext.getConversationId();
/*  67 */       if (!this.seenConvIDs.contains(Integer.valueOf(convId)))
/*     */       {
/*     */ 
/*     */ 
/*  71 */         ConversationHit curHit = null;
/*  72 */         if ((opNext instanceof ConversationHit)) {
/*  73 */           curHit = (ConversationHit)opNext;
/*  74 */         } else if ((opNext instanceof MessageHit)) {
/*  75 */           curHit = ((MessageHit)opNext).getConversationResult();
/*  76 */         } else if ((opNext instanceof MessagePartHit)) {
/*  77 */           curHit = ((MessagePartHit)opNext).getMessageResult().getConversationResult();
/*     */         }
/*  79 */         else if (!$assertionsDisabled) { throw new AssertionError();
/*     */         }
/*  81 */         this.seenConvIDs.add(Integer.valueOf(convId));
/*     */         
/*     */ 
/*     */ 
/*  85 */         while (this.results.hasNext()) {
/*  86 */           ZimbraHit nextHit = this.results.peekNext();
/*  87 */           if (!isConversation(nextHit)) {
/*  88 */             return curHit;
/*     */           }
/*  90 */           if (nextHit.getConversationId() != convId) {
/*  91 */             return curHit;
/*     */           }
/*     */           
/*  94 */           this.results.getNext();
/*     */           
/*     */ 
/*  97 */           if ((nextHit instanceof MessageHit)) {
/*  98 */             curHit.addMessageHit((MessageHit)nextHit);
/*  99 */           } else if ((nextHit instanceof MessagePartHit)) {
/* 100 */             curHit.addMessageHit(((MessagePartHit)nextHit).getMessageResult());
/*     */           }
/*     */         }
/*     */         
/*     */ 
/* 105 */         return curHit;
/*     */       }
/*     */     }
/* 108 */     return null;
/*     */   }
/*     */   
/*     */   private boolean isConversation(ZimbraHit hit) {
/* 112 */     return ((hit instanceof MessageHit)) || ((hit instanceof MessagePartHit)) || ((hit instanceof ConversationHit));
/*     */   }
/*     */   
/*     */   private boolean bufferNextHit() throws ServiceException {
/* 116 */     if (this.nextHit == null) {
/* 117 */       this.nextHit = internalGetNextHit();
/*     */     }
/* 119 */     return this.nextHit != null;
/*     */   }
/*     */   
/*     */   public ZimbraHit peekNext() throws ServiceException
/*     */   {
/* 124 */     bufferNextHit();
/* 125 */     return this.nextHit;
/*     */   }
/*     */   
/*     */   public void resetIterator() throws ServiceException
/*     */   {
/* 130 */     this.seenConvIDs.clear();
/* 131 */     this.results.resetIterator();
/* 132 */     this.nextHitNo = 0;
/* 133 */     this.cachedResults.clear();
/* 134 */     this.nextHit = null;
/*     */   }
/*     */   
/*     */   public ZimbraHit getNext() throws ServiceException
/*     */   {
/* 139 */     ZimbraHit hit = null;
/* 140 */     if (this.cachedResults.size() > this.nextHitNo) {
/* 141 */       hit = (ZimbraHit)this.cachedResults.get(this.nextHitNo);
/* 142 */     } else if (bufferNextHit()) {
/* 143 */       hit = this.nextHit;
/* 144 */       this.cachedResults.add(this.nextHitNo, this.nextHit);
/* 145 */       this.nextHit = null;
/*     */     }
/* 147 */     if (hit != null) {
/* 148 */       this.nextHitNo += 1;
/*     */     }
/* 150 */     return hit;
/*     */   }
/*     */   
/*     */   public void close() throws IOException
/*     */   {
/* 155 */     this.results.close();
/*     */   }
/*     */   
/*     */   public ZimbraHit skipToHit(int hitNo) throws ServiceException
/*     */   {
/* 160 */     if (hitNo == 0) {
/* 161 */       resetIterator();
/* 162 */       return getNext();
/*     */     }
/* 164 */     ZimbraHit hit = null;
/* 165 */     if (hitNo < this.cachedResults.size()) {
/* 166 */       this.nextHitNo = hitNo;
/*     */     } else {
/* 168 */       this.nextHitNo = this.cachedResults.size();
/*     */     }
/* 170 */     while (this.nextHitNo <= hitNo) {
/* 171 */       hit = getNext();
/* 172 */       if (hit == null) {
/*     */         break;
/*     */       }
/*     */     }
/* 176 */     return hit;
/*     */   }
/*     */   
/*     */ 
/*     */   public List<QueryInfo> getResultInfo()
/*     */   {
/* 182 */     return this.results.getResultInfo();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ConvQueryResults.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */